/**
 * @(#)JDOTuanGouChannelStorage.java, 2010-5-14. 
 * 
 */
package com.rsstuan.storage.impl;

import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;

import com.rsstuan.storage.data.TuanGouChannelData;

/**
 *
 * @author guosq
 *
 */
public class JDOTuanGouChannelStorage {
   
    private static final Logger LOG = Logger.getLogger(JDOTuanGouChannelStorage.class.getName());
    
    public boolean saveChannel(TuanGouChannelData channel) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            pm.makePersistent(channel);
            return true;
        } 
        catch (Exception e) {
            return false;
        }
        finally {
            pm.close();
        }
    }
    
    public List<TuanGouChannelData> getAllChannels() {
        PersistenceManager pm = PMF.get().getPersistenceManager(); 
        String query = "select from " + TuanGouChannelData.class.getName();
        List<TuanGouChannelData> channels = (List<TuanGouChannelData>) pm.newQuery(query).execute();
        return channels;
    }
    
    public TuanGouChannelData getChannel(Long id) {
         PersistenceManager pm = PMF.get().getPersistenceManager(); 
            String query = "select from " + TuanGouChannelData.class.getName()
                    + " where id ==" + id;
            List<TuanGouChannelData> channels = (List<TuanGouChannelData>) pm.newQuery(query).execute();
            if (channels != null && channels.size() > 0)
            	return channels.get(0);
            else 
            	return null;
    }
    
    public TuanGouChannelData getChannel(Long siteId, String city) {
        PersistenceManager pm = PMF.get().getPersistenceManager(); 
           String query = "select from " + TuanGouChannelData.class.getName()
                   + " where siteId ==" + siteId + " && city == '" + city + "'";
           TuanGouChannelData channel = (TuanGouChannelData) pm.newQuery(query).execute();
           return channel;
   }
    
    public List<TuanGouChannelData> getInAcitveChannels() {
        PersistenceManager pm = PMF.get().getPersistenceManager(); 
           String query = "select from " + TuanGouChannelData.class.getName()
                   + " where active == false";
           List<TuanGouChannelData> channels = (List<TuanGouChannelData>) pm.newQuery(query).execute();
           return channels;
   }
    
    public List<TuanGouChannelData> getAcitveChannels() {
        PersistenceManager pm = PMF.get().getPersistenceManager(); 
           String query = "select from " + TuanGouChannelData.class.getName()
                   + " where active == true";
           List<TuanGouChannelData> channels = (List<TuanGouChannelData>) pm.newQuery(query).execute();
           return channels;
   }


    public List<TuanGouChannelData> getAllChannelsBySiteId(Long siteId) {
        PersistenceManager pm = PMF.get().getPersistenceManager(); 
        String query = "select from " + TuanGouChannelData.class.getName()
                + " where siteId ==" + siteId;
        List<TuanGouChannelData> channels = (List<TuanGouChannelData>) pm.newQuery(query).execute();
        return channels;
    }
    
    public boolean activeChannels(Long[] channelIds) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            Arrays.sort(channelIds);
            String query = "select from " + TuanGouChannelData.class.getName()
                    + " where active == false";
            List<TuanGouChannelData> channels = (List<TuanGouChannelData>) pm.newQuery(query).execute();
            for (TuanGouChannelData channel : channels) {
                if (Arrays.binarySearch(channelIds, channel.getId()) >= 0) {
                    channel.setActive(true);
                }
            }
            return true;
        }
        catch (Exception e) {
            LOG.log(Level.WARNING, "activeChannels failed", e);
            return false;
        }
        finally {
            pm.close();
        }
    }

    /**
     * @param object
     * @return
     */
    public boolean deactive(Long channelId) {
        PersistenceManager pm = PMF.get().getPersistenceManager(); 
        try {
            String query = "select from " + TuanGouChannelData.class.getName()
                    + " where active == true";
            if (channelId != null) {
                query += " and channelId ==" + channelId;
            }
            List<TuanGouChannelData> channels = (List<TuanGouChannelData>) pm.newQuery(query).execute();
            for (TuanGouChannelData channel : channels) {
                channel.setActive(false);
            }
            return true;
        }
        catch (Exception e) {
            LOG.log(Level.WARNING, "deative channel failed", e);
            return false;
        }
        finally {
            pm.close();
        }
    }
    
    /**
     * 如果ChannelID在channdIds内，则置为active,否则置为inActive
     * @param channelIds
     * @return
     */
    public boolean changeChannelStatus(Long channelIds[]) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            Arrays.sort(channelIds);
            String query = "select from " + TuanGouChannelData.class.getName();
            List<TuanGouChannelData> channels = (List<TuanGouChannelData>) pm.newQuery(query).execute();
            for (TuanGouChannelData channel : channels) {
                if (Arrays.binarySearch(channelIds, channel.getId()) >= 0) {
                    channel.setActive(true);
                } else {
                    channel.setActive(false);
                }
            }
            return true;
        }
        catch (Exception e) {
            LOG.log(Level.WARNING, "changeChannelStatus failed", e);
            return false;
        }
        finally {
            pm.close();
        }
    }

	public boolean updateLastUpdateTime(Long channelId) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			String query = "select from " + TuanGouChannelData.class.getName()
					+ " where id =" + channelId;
			TuanGouChannelData channel = (TuanGouChannelData) pm
					.newQuery(query).execute();
			channel.setLastUpdateTime(System.currentTimeMillis());
			return true;
		} catch (Exception e) {
			LOG.log(Level.WARNING, "changeChannelStatus failed", e);
			return false;
		} finally {
			pm.close();
		}
	}
		

}
